package edu.bu.cs673.superkids123.parents.view.dialog;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.GroupLayout.Alignment;

import edu.bu.cs673.superkids123.common.model.AchievementRule;
import edu.bu.cs673.superkids123.common.model.AchievementRule.Activity;
import edu.bu.cs673.superkids123.common.model.AchievementRule.Compare;
import edu.bu.cs673.superkids123.common.model.AchievementRule.Metric;

public class AchievementRuleDialog extends JDialog implements ActionListener, ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel panel, cards;
	private JComboBox activityCombo, metricCombo, conditionCombo;
	private JTextField integerField, hourField, minuteField;
	private JButton okButton, cancelButton;
	
	private AchievementRule rule;

	final static String INTEGER_PANEL = "Card for Integers";
	final static String TIME_PANEL = "Card for Time";

	public AchievementRuleDialog(JFrame frame, boolean modal, AchievementRule rule) {
		super(frame, modal);
		panel = new JPanel();
		getContentPane().add(panel);
		
		// Activity
		JLabel activityLabel = new JLabel("Activity");
		activityCombo = new JComboBox(AchievementRule.Activity.values());
		
		// Metric
		JLabel metricLabel = new JLabel("Metric");
		metricCombo = new JComboBox(AchievementRule.Metric.values());
		metricCombo.addItemListener(this);
		
		// Condition
		JLabel conditionLabel = new JLabel("Condition");
		conditionCombo = new JComboBox(AchievementRule.Compare.values());
		
		// Value
		JLabel valueLabel = new JLabel("Value");
		
		// Integer Card
		JPanel integerCard = new JPanel();
		integerCard.add(new JLabel("Number (1-100):"));
		integerField = new JTextField("", 10);
		integerCard.add(integerField);
		
		// Timespan Card
		JPanel timeCard = new JPanel();
		timeCard.add(new JLabel("Hours:"));
		hourField = new JTextField("", 5);
		timeCard.add(hourField);
		timeCard.add(new JLabel("Mins:"));
		minuteField = new JTextField("", 5);
		timeCard.add(minuteField);
		
		// Card Panel
		cards = new JPanel(new CardLayout());
		cards.add(integerCard, INTEGER_PANEL);
		cards.add(timeCard, TIME_PANEL);
		
		// OK/Cancel
		okButton = new JButton("OK");
		okButton.addActionListener(this);
		
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		
		// Layout items
		GroupLayout layout = new GroupLayout(panel);
		
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
							.addComponent(activityLabel)
							.addComponent(activityCombo, 100, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(metricLabel)
						.addComponent(metricCombo, 100, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(conditionLabel)
						.addComponent(conditionCombo, 100, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(valueLabel)
						.addComponent(cards, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(layout.createSequentialGroup()
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(okButton)
							.addComponent(cancelButton))))
		);
		
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(Alignment.TRAILING)
						.addComponent(metricLabel)
						.addComponent(activityLabel)
						.addComponent(conditionLabel)
						.addComponent(valueLabel))
					.addGroup(layout.createParallelGroup(Alignment.CENTER)
						.addComponent(metricCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(activityCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(conditionCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(cards, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(okButton)
						.addComponent(cancelButton)))
		);
		
		panel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		this.rule = rule;
		fillRuleFields();
		
		pack();
		setLocationRelativeTo(frame);
	}
	
	public AchievementRule getRule() {
		return rule;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == okButton) {
			createRule();
			this.setVisible(false);
		} else if (e.getSource() == cancelButton) {
			rule = null;
			this.setVisible(false);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
        CardLayout cl = (CardLayout)(cards.getLayout());
        Metric selected = (Metric) e.getItem();
        
        if (selected == Metric.Time)
        	cl.show(cards, TIME_PANEL);
        else
        	cl.show(cards, INTEGER_PANEL);
       
	}
	
	private void createRule() {
		rule = new AchievementRule();
		
		rule.setActivity((Activity) activityCombo.getSelectedItem());
		rule.setMetric((Metric) metricCombo.getSelectedItem());
		rule.setComparer((Compare) conditionCombo.getSelectedItem());
		
		if (rule.getMetric() == Metric.Time) {
			int hours = Integer.parseInt(hourField.getText());
			int minutes = Integer.parseInt(minuteField.getText());
			
			long miliseconds = hours * 3600000;
			miliseconds += minutes * 60000;
			
			rule.setValue(new Date(miliseconds));	
		} else {
			int number = Integer.parseInt(integerField.getText());
			rule.setValue(number);
		}
	}
	
	private void fillRuleFields() {
		
		if (rule == null)
			return;
		
		activityCombo.setSelectedItem(rule.getActivity());
		metricCombo.setSelectedItem(rule.getMetric());
		conditionCombo.setSelectedItem(rule.getComparer());
		
		if (rule.getMetric() == Metric.Time) {
			long seconds = ((Date) rule.getValue()).getTime() / 1000;
			long hours = seconds / 3600;
			long minutes = (seconds % 3600) / 60;
			
			hourField.setText(String.format("%s", hours));
			minuteField.setText(String.format("%s", minutes));
		} else {
			String value = String.format("%s", rule.getValue());
			integerField.setText(value);
		}
	}
}
